package com.cxd.base.util.wtj;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang.StringUtils;

import com.cxd.base.common.exception.CxdException;
import com.cxd.base.common.exception.ExceptionEnum;


/**
 * @author wtj
 * @date 2015年12月1日16:40:34
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
public class BUtils {
	/**
	 * 给实体属性赋值  如 TestBean 有id ,赋予 1001值
	 */
	public static void setBean(Object classzs, String attrName, Object attrValue)throws IllegalArgumentException,IllegalAccessException {
		Field[] fields = classzs.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if(fields[i].getName().equals(attrName)){
				fields[i].setAccessible(true);
				setBean(classzs,fields[i],attrValue);
			}
		}
	}
	public static void setBean(Object classzs,Map<String,Object> map)throws IllegalArgumentException,IllegalAccessException {
		Field[] fields = classzs.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				setBean(classzs,fields[i],map.get(fields[i].getName()));
		}
	}
	
	public static <T> List<T> tableListMapToBean(List<Map<String,Object>> listMap,Class<T> clazz){
		List<T> listT = new ArrayList<T>();
		for(Map<String,Object> map:listMap){
			listT.add(tableMapToBean(map,clazz));
		}
		return listT;
	}
	/**
	 * Map 转 实体Bean ，Map 为数据查询出来的数据,字段都为大写 如 : ID,NAME,USER_ID
	 */
	public static <T> T tableMapToBean(Map<String,Object> map,Class<T> clazz)throws CxdException{
		if(map == null){
			return null;
		}
		Field[] fields = clazz.getDeclaredFields();
		T bean;
		try {
			bean = (T) clazz.newInstance();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new CxdException(ExceptionEnum.IllegalAccessException.getExceDes());
		} catch (InstantiationException e) {
			e.printStackTrace();
			throw new CxdException(ExceptionEnum.InstantiationException.getExceDes());
		}
		for (int i = 0; i < fields.length; i++) {
			fields[i].setAccessible(true);
			String key = SUtils.getHumpToUpperCase(fields[i].getName());
			Object value = map.get(key);
			if(value == null){
				value = map.get(key.toLowerCase());
			}
			setBean(bean,fields[i],value);
		}
		return bean;
	}
	
	/**
	 * 给实体属性赋值  如 TestBean 有id ,赋予 1001值
	 */
	public static void setBean(Object classzs,Field field,Object value){
		try {
			if (String.class == field.getType()) {
				field.set(classzs,value==null?null:value.toString());
			}else if (Date.class ==field.getType()) {
				field.set(classzs, value==null?null:(Date)value);
			}else if (Integer.class == field.getType()){
				field.set(classzs, value==null?null:Integer.parseInt(value.toString()));
			}else if (Long.class == field.getType()){
				field.set(classzs, value==null?null:Long.parseLong(value.toString()));
			}else if (Float.class == field.getType()){
				field.set(classzs, value==null?null:Float.parseFloat(value.toString()));
			}else if (Double.class == field.getType()){
				field.set(classzs, value==null?null:Double.parseDouble(value.toString()));
			}else if(Timestamp.class == field.getType() ){
				field.set(classzs, value==null?null:(Timestamp)value);
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			throw new CxdException(ExceptionEnum.IllegalArgumentException.getExceDes());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new CxdException(ExceptionEnum.IllegalAccessException.getExceDes());
		}
	}
	
//	public static <T> T convertMap(Map<String,Object> map,Class<T> type)throws IntrospectionException, IllegalAccessException,
//    InstantiationException, InvocationTargetException {
//		BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
//		T bean = (T)type.newInstance(); // 创建 JavaBean 对象
//		// 给 JavaBean 对象的属性赋值
//		PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
//		for (int i = 0; i< propertyDescriptors.length; i++) {
//		    PropertyDescriptor descriptor = propertyDescriptors[i];
//		    String propertyName = descriptor.getName();
//		    if (map.containsKey(propertyName)) {
//		        Object value = map.get(propertyName);
//		        Object[] args = new Object[1];
//		        if(Integer.class == descriptor.getPropertyType()){
//		        	args[0] = (Integer)value;
//		        }else if(String.class == descriptor.getPropertyType()){
//		        	args[0] = value;
//		        }
//		        descriptor.getWriteMethod().invoke(bean, args);
//		    }
//		}
//		return bean;
//	}
	//将javabean转为map类型，然后返回一个map类型的值
	public static Map<String, Object> beanToMap(Object obj) { 
	    Map<String, Object> params = new HashMap<String, Object>(0); 
	    try { 
	      PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); 
	      PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); 
	      for (int i = 0; i < descriptors.length; i++) { 
	        String name = descriptors[i].getName(); 
	        if (!StringUtils.equals(name, "class")) { 
	          params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); 
	        } 
	      } 
	    } catch (Exception e) { 
	      e.printStackTrace(); 
	    } 
	    return params; 
	}
}
